package com.library.evaluate.dao;

import com.framework.BaseDao;
import com.framework.Page;
import com.library.entity.Book;
import com.library.entity.Evaluate;
import com.library.entity.Staff;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Repository
public class EvaluateDaoImpl extends BaseDao<Evaluate,String> {
	@Resource
	private SessionFactory sessionFactory;

	public List<Evaluate> findByStaff(Staff staff){
		try{
			return super.findByProperty("from Evaluate e where e.staff=?", new Object[]{staff});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public List<Evaluate> findByBook(Book book){
		try{
			return super.findByProperty("from Evaluate e where e.book=?", new Object[]{book});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public List<Evaluate> findByBookOrderByPriaseDesc(Book book){
		try{
			return super.findByProperty("from Evaluate e where e.book=? order by e.praise desc", new Object[]{book});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/*分页*/
	public Page<Evaluate> findByBookOrderByPriaseDesc(int pageNum,int pageSize,Book book){
		try{
			return super.findByPage(pageNum,pageSize,"from Evaluate e where e.book=? order by e.praise desc", new Object[]{book});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public List<Evaluate> findByBookOrderByForbiddenDesc(Book book){
		try{
			return super.findByProperty("from Evaluate e where e.book=? order by e.forbidden desc", new Object[]{book});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/*分页*/
	public Page<Evaluate> findByBookOrderByForbiddenDesc(int pageNum,int pageSize,Book book){
		try{
			return super.findByPage(pageNum,pageSize,"from Evaluate e where e.book=? order by e.forbidden desc", new Object[]{book});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*分页*/
	public Page<Evaluate> findAllOrderByForbiddenDesc(int pageNum,int pageSize){
		try{
			return super.findByPage(pageNum,pageSize,"from Evaluate e order by e.forbidden desc", new Object[]{});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*不分页*/
	public List<Evaluate> findAllOrderByForbiddenDesc(){
		try{
			return super.findByProperty("from Evaluate e order by e.forbidden desc", new Object[]{});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public Evaluate getEvaluateById(Integer evaluateId){
		try{
			return super.get(evaluateId);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public List<Evaluate> findByBookAndStaff(Book book,Staff staff){
		try{
			return super.findByProperty("from Evaluate e where e.book=? and e.staff=?", new Object[]{book,staff});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*查找条件评论数量*/
	public Long findCountByProperty(Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from Evaluate e");
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and e."+ key + "=?");
					else{
						hql.append(" and e."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" e."+ key + "=?");
					else{
						hql.append(" e."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				}
				++i;
				++j;
			}
		}
		try{
			return this.getEvaluatesCount(hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return (long)0;
		}
	}

	/*按条件查询数量*/
	public Long getEvaluatesCount(String hql,Object[] params){
		try{
			return super.findCountByPage("select count(*) " + hql,params);
		}catch(Exception e){
			e.printStackTrace();
			return (long)0;
		}
	}

	public void saveEvaluate(Evaluate evaluate){
		try {
			this.save(evaluate);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updateEvaluate(Evaluate evaluate){
		try {
			this.update(evaluate);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<Evaluate> getAll(Book book){
		try{
			return super.findByProperty("from Evaluate e where e.book =?", new Object[]{book});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public List<Evaluate> getAllOrderByPraiseDesc(Book book){
		try{
			return super.findByProperty("from Evaluate e where e.book =? order by e.praise desc", new Object[]{book});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public List<Evaluate> getAllOrderByPraiseDesc(){
		try{
			return super.findByProperty("from Evaluate e order by e.praise desc", new Object[]{});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public List<Evaluate> getTopThreeOrderByPraiseDesc(Book book){
		Object[] params = new Object[]{book};
		String hql = "from Evaluate e where e.book =? order by e.praise desc";
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);

		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
				query.setMaxResults(3);	//取前三条记录
			}
		}
		return query.list();
	}

	public void deleteEvaluate(Evaluate evaluate){
		try {
			this.delete(evaluate);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
